"use strict";

const sequelize = require("./db").sequelize();
const User = sequelize.import("./user");
const UserCheckin = sequelize.import("./userCheckin");
const UserAddress = sequelize.import("./userAddress");
const Role = sequelize.import("./role");
const Author = sequelize.import("./author");

// 建立模型之间的关系
User.hasOne(UserCheckin, { as: "UserCheckin" });
UserCheckin.belongsTo(User);
User.hasMany(UserAddress, {
  foreignKey: "user_id",
  targetKey: "id",
  as: "Address"
});
User.belongsToMany(Role, { through: "userRoles", as: "UserRoles" });
Role.belongsToMany(User, { through: "userRoles", as: "UserRoles" });

// User.belongsToMany(Author, { through: "userAuthors", as: "UserAuthors" });
// Author.belongsToMany(User, { through: "userAuthors", as: "UserAuthors" });

Role.belongsToMany(Author, { through: "roleAuthors", as: "RoleAuthors" });
Author.belongsToMany(Role, { through: "roleAuthors", as: "RoleAuthors" });

// 同步模型到数据库中
sequelize.sync({
  force: true
});

module.exports = {
  sequelize,
  User,
  UserCheckin,
  UserAddress,
  Role,
  Author
};
